package com.lmx.web.controller.thirdPart;

import com.lmx.persistence.service.HealthInfoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedOutputStream;
import java.io.IOException;

/*-
 * 处理接口
 */

@Controller
@RequestMapping("/access/etcomm")
public class EtcommController {
	private static Log logger = LogFactory.getLog(EtcommController.class);
	@Autowired
	private HealthInfoService healthInfoService;

	@RequestMapping("/healthData")
	@ResponseBody
	public void bedMatWarningData(HttpServletRequest request, HttpServletResponse response) {
		String result = "";
		//result="[{ \"apptype\" : \"Spo2DataV1\", \"datakey\" : \"020000000032200393080104162120012600000000000017c7f722\", \"adddate\" : \"2017-06-26 20:06:40\", \"EntAccount\" : \"testEnterprise\", \"DataId\" : \"6yDRB7X7QE4\", \"collectdate\" : \"2017-06-26 20:06:38\", \"pulse\" : 64, \"oxygen\" : 98 }]";
		// \"020000000013000525030101142900002800000000271001920001\",
		logger.debug("-----------------------------获取益体康健康数据接口-------------------------------------");
//		BufferedInputStream bufin;
//		try {
//	        logger.debug("etcommtest1 ");
//			bufin = new BufferedInputStream(request.getInputStream());
//	        int buffSize = 1024;
//	        ByteArrayOutputStream out1 = new ByteArrayOutputStream(buffSize);
//	        byte[] temp = new byte[buffSize];
//	        int size = 0;
//	        logger.debug("etcommtest2 ");
//	        while ((size = bufin.read(temp)) != -1) {
//	            out1.write(temp, 0, size);
//	        }
//	        bufin.close();
//	        byte[] content = out1.toByteArray();
//	        logger.debug("etcommtest "+new String(content));
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}	
//		try {
//			BufferedReader in = null;
//			String result1 = "";
//
//			// 定义BufferedReader输入流来读取URL的响应
//			in = new BufferedReader(new InputStreamReader(request.getInputStream()));
//			String line;
//			while ((line = in.readLine()) != null) {
//				result1 += line;
//			}
//			logger.debug("etcommtest "+result1);
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}	
		
		logger.debug("etcomm0 -----" + request.getParameter("datas")+" "+request.getRequestURL()+" "+request.getRequestURI());
		if (request.getParameter("datas") != null) {
		    result = request.getParameter("datas");
			JSONArray jsonArray = JSONArray.fromObject(result);
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				healthInfoService.addEtcHealthInfo(object);
			}
			logger.debug("etcommHealthData: " + result);
		}
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("http ok".getBytes());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.debug("bedMatWarningData error " + e);
		}
	}
}
